import {request} from "../../request/index";
import regeneratorRuntime from '../../lib/runtime/runtime';
import {login} from "../../utils/asyncWX";

Page({
    async handleGetUserInfo(e) {
        try {
            //获取用户信息
            const {encryptedData, iv, rawData, signature} = e.detail;
            //获取小程序登录成功后的code
            const {code} = await login();
            //发送请求获取用户的token
            const params = {encryptedData, iv, rawData, signature, code};
            //必须为企业账号下面才能拿到token
            const {token} = await request({
                method: 'post',
                url: 'users/wxlogin',
                data: params
            });
            //将token放入缓存 同时跳转回上一个页面
            wx.setStorageSync('token', token);
            wx.navigateBack({delta: 1});
        } catch (err) {
            console.log(err);
            wx.setStorageSync('token', 'token is empty');
            wx.navigateBack({delta: 1});
        }
    }
})
